package quartz.job2;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class FirstJobScheduler {
	public static void main(String[] args) throws SchedulerException {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		Date date=new Date();
		System.out.println("start time:"+sdf.format(date));
		date.setTime(date.getTime()+3000);
		Date endDate=new Date();
		endDate.setTime(endDate.getTime()+6000);
		//1、创建JobDetail实例，该实例与FirstJob.class绑定,分组缺失则默认为default
		JobDetail jobDetail = JobBuilder
				.newJob(FirstJob.class)
				.withIdentity("firstJob", "group1")				
				.build();
		//2、创建一个Trigger实例，定义该job 当前时间3秒后执行，间隔3秒，直到6秒后。。。
		//此group1分组和上group1分组不是同一个组；各自属于不同实例下的分组
		Trigger trigger=TriggerBuilder.newTrigger()
				.withIdentity("trigger", "group1")
				.startAt(date)
				.endAt(endDate)
				.withSchedule(
					SimpleScheduleBuilder.simpleSchedule()
					.withIntervalInSeconds(2)
					.repeatForever()
						)
				.build();
		SchedulerFactory sf=new StdSchedulerFactory();
		Scheduler scheduler=sf.getScheduler();
		scheduler.start();
		//3、传入两个实例执行
		scheduler.scheduleJob(jobDetail, trigger);
		
		
	}
}
